package com.sduept.nwld.dataserver.manager.etl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.TreeSet;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sduept.bigdata.fault.common.ETLDAO;
import com.sduept.core.dao.SuperDAO;
import com.sduept.generator.ObjectId;
import com.sduept.nwld.dataserver.manager.cim.BxCimManager;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class BxSubStationETLManager {

	@Autowired
	private SuperDAO dao;
	
	@Autowired
	private ETLDAO eDao;
	@Autowired
	private BxCimManager bxCm;
	
	public  void receiveAndSaveAllData() {
		List<?> list = getAllSourceData();
		receiveAndSave(list);
	}
	public  void receiveAndSavePartData(String date) {
		List<?> list = getAllSourceData(date);
		if(!list.isEmpty()) {
			receiveAndSave(list);
		}
			
	}
	
	
	public List<?> getAllSourceData(){
		List <?>result = eDao.query("select mRID,mName,stationID from t_bx_substation ");
		return result;
	}
	public List<?> getAllSourceData(String date) {
		String sql = "select mRID,mName,stationID from t_bx_substation "
				+ " where update_time=";
		List<?> result = eDao.query(sql + "'" + date + "'");
		return result;
	}
	public  void receiveAndSave(List<?> list) {
		ETLDataSave equipmentSave = new ETLDataSave(list);
		equipmentSave.saveToBean();
	}
	public class ETLDataSave{
		private List<?> list;
		private List<String> ids=new ArrayList<String>();
 
		public ETLDataSave(List<?> list) {
			super();
			this.list = list;
			String sql="SELECT CONCAT(mName,stationID) FROM t_bx_substation";
			List<?> nativeQuery = dao.nativeQuery(sql);
			//name+stationid 作为唯一标识，保信变电站中有id不同，变电站名称和变电站id相同的情况
			ids = (List<String>)nativeQuery;
		}
		public void saveToBean(){
			String sql="insert into t_bx_substation(mRID,mName,stationID,id) values(?,?,?,?)";
			 List<Object[]> datas = new ArrayList<Object[]>();
			 //保信变电站中有id不同，变电站名称和变电站id相同的情况,过滤相同的名字和变电站id的记录
			 List<?> unique = list.stream().collect(
			           Collectors. collectingAndThen(
			                    Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o -> ((Object[])o)[1] + ";" + ((Object[])o)[2]))), ArrayList::new)
			);
			for(Object obj : unique){
				Object[] objs = (Object[]) obj;
				Object[] saveObjs=new Object[4];
				for(int i=0;i<objs.length;i++){
					if(objs[i]!=null&&"null".equals(objs[i].toString())){
						objs[i]=null;
					}
					saveObjs[i]=objs[i];
				}
				if(ids.contains(saveObjs[1].toString()+saveObjs[2].toString())){
					continue;
				}
				saveObjs[3]=ObjectId.get().toString();
				datas.add(saveObjs);
			}
			try {
				dao.persistentBatch(sql, datas);
				log.info("更新保信变电站："+datas.size());
				log.info(bxCm.relationSubstation());
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
}
